package com.atguigu.vod.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.Utils.aliyunVodUtils.GetPlayAuthUtils;
import com.atguigu.vod.service.VodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/filevod")
@CrossOrigin  // 解决跨域
@ Slf4j
public class VodController {
    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadAlyiVideo")
    public Result uploadAlyiVideo(MultipartFile file){
        String videoId = null;
        try {
            videoId = vodService.uploadAlyiVideo(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok().data("videoId",videoId);
    }
    //单个删除
    @DeleteMapping("removeaAlyVideo/{videoId}")
    public Result removeAlyVideo(@PathVariable String videoId){
        log.info(videoId);
        try{
            vodService.removeAlyVideo(videoId);
            return Result.ok();

        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }
    //批量删除
    @DeleteMapping("removeaAlyVideoBatch}")
    public Result removeAlyVideoBatch(@RequestParam List<String> videoIdList){
//        log.info(videoIdList);
        try{
            vodService.deleteBatch(videoIdList);
            return Result.ok();

        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }

    //获取播放凭证
    @GetMapping("getAliVideoAuth/{videoId}")
    public Result getAliVideoAuth(@PathVariable String videoId){

        try {
            log.info("哈哈" + videoId);
            String playAuth = GetPlayAuthUtils.getPlayAuth(videoId);
            return Result.ok().data("playAuth",playAuth);
        } catch (Exception e) {
            throw new GuliException(20001,"获取播放凭证失败");
        }
    }
}
